package cn.liangcaomei.eduservice.controller;


import cn.liangcaomei.commonutils.R;
import cn.liangcaomei.eduservice.entity.EduCourse;
import cn.liangcaomei.eduservice.entity.vo.CourseInfoVo;
import cn.liangcaomei.eduservice.entity.vo.CoursePublishVo;
import cn.liangcaomei.eduservice.entity.vo.CourseQueryVo;
import cn.liangcaomei.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author LiuJie
 * @since 2024-01-27
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;

    // 课程列表 基本实现
    // 带分页查询
    @ApiOperation(value = "分页查询")
    @PostMapping("{current}/{limit}")
    public R getCourseList(@PathVariable long current, @PathVariable long limit,
                           @RequestBody(required = false)CourseQueryVo courseQueryVo){
        //1 创建 page 对象
        Page<EduCourse> coursePage = new Page<>(current, limit);

        //2 构建条件
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();

        //3 多条件查询，判断条件是否为空，不为空，则拼接查询
        String title = courseQueryVo.getTitl();
        String status = courseQueryVo.getStatus();

        if (!StringUtils.isEmpty(title)) {
            wrapper.like("title",title);
        }
        if (!StringUtils.isEmpty(status)) {
            wrapper.like("status",status);
        }

        //sort
        wrapper.orderByDesc("gmt_create");

        //调用方法，实现条件带分页查询
        eduCourseService.page(coursePage,wrapper);

        //获取总记录数
        long total = coursePage.getTotal();

        //获取记录数
        List<EduCourse> records = coursePage.getRecords();

        return R.ok().data("total",total).data("list",records);
    }

    //添加課程信息方法
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        String id = eduCourseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId",id);
    }

    //根据课程id，查询课程信息
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo = eduCourseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }

    //修改课程信息
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        eduCourseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    //根据课程id查询课程的确认信息
    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id){
        CoursePublishVo coursePublishVo = eduCourseService.publishCourseInfo(id);
        return R.ok().data("publishCourse",coursePublishVo);
    }

    //最终发布
    //修改课程状态
    @GetMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Nurmal");
        boolean flag = eduCourseService.updateById(eduCourse);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //删除课程
    @DeleteMapping("{courseId}")
    public R deleteCourse(@PathVariable String courseId){
        eduCourseService.removeCourse(courseId);
        return R.ok();
    }

}

